﻿namespace DataIntegration.Utils.Serialization
{
    using System.IO;
    using System.Text;
    using System.Xml.Serialization;

    public static class SerializationFactory
    {
        public static string SerializeToXmlString<T>(T message)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            StringBuilder stringBuilder = new StringBuilder();

            using (StringWriter writer = new StringWriter(stringBuilder))
            {
                serializer.Serialize(writer, message);
            }

            return stringBuilder.ToString();
        }

        public static T DeserializeFromXml<T>(string xml)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));

            using (StringReader reader = new StringReader(xml))
            {
                return (T) serializer.Deserialize(reader);
            }
        }
    }
}
